<?php
namespace app\blog\model;
use think\db;
use think\Model;
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
class CountModel extends Model
{

    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->session=$this->session();
    }

    /*
     * 商品列表
     * */
    public function session()
    {
        $id=cmf_get_current_admin_id();
        $where['agency_id']=$id;
        $session=DB::name('shop')->where($where)->find();
        return $session;
    }
    public function index($cause)
    {
        /*条件编写*/
        $session=$this->session();
        $where='or.shop_id='.$session['id'];
        if(isset($cause['pay_type']))
        {
            if($cause['pay_type']>0)
            {
              //  $where['or.pay_type']=$cause['pay_type'];
                $where.=" and  or.pay_type =".$cause['pay_type'];
            }
        }else{
            $cause['pay_type']='';
        }
        if(isset($cause['order_no']))
        {
            if($cause['order_no'])
            {
              //  $where['or.order_no']=$cause['order_no'];
                $where .=" and or.order_no =".$cause['order_no'];
            }
        }else{
            $cause['order_no']='';
        }
        if(isset($cause['pay_status']))
        {
            if($cause['pay_status']!=0)
            {
              //  $where['or.pay_status']=$cause['pay_status'];
                $where.=" and or.pay_status = ".$cause['pay_status'];
            }
        }else{
            $cause['pay_status']='';
        }
        if(isset($cause['refunt']))
        {
            if($cause['refunt']!=0)
            {
               // $where['or.refunt']=$cause['refunt'];
                $where.=" and or.refunt  = ".$cause['refunt'];
            }
        }else{
            $cause['refunt']='';
        }
        if(isset($cause['start_time']) && isset($cause['end_time']))
        {
            if(!empty($cause['start_time']))
            {
               $start=strtotime($cause['start_time']);
               if(!empty($cause['end_time']))
               {
                   $end=strtotime($cause['end_time']);
               }else{
                   $end=$start+86400;
               }
               $where.=' and or.w_time  >='.$start;
               $where.=' and or.w_time  <='.$end;
            }
        }else{
            $cause['start_time']='';
            $cause['end_time']='';
        }
        $data=DB::name('order')->alias('or')->join(' member me ','me.id = or.member_id')->join('shop sh','sh.id = or.shop_id')->join('pay p','or.pay_type = p.id')->where($where)->field('or.*,me.nick_name,sh.shop_name,p.name')->paginate(10);
        /*对数据进行封装*/
        if($data->toArray())
        {

            foreach ($data as $k=>$v)
            {
                $date=array();
                $date=$v;
                if($date['refunt']==1)
                {
                    $date['refunt_name']='退款';
                }else{
                    $date['refunt_name']='正常';
                }
                switch ($date['pay_status'])
                {
                    case 1;
                        $date['pay_name']='支付';
                        break;
                    case 2;
                        $date['pay_name']='预约支付';
                        break;
                    case -3;
                        $date['pay_name']='未支付';
                        break;
                    case -1;
                        $date['pay_name']='挂单';
                        break;
                    case -2;
                        $date['pay_name']='订单关闭';
                        break;
                    default:
                        $date['pay_name']='订单出错';
                }
                $data->offsetSet($k,$date);

            }
        }
        /*分页保存条件*/
        $data->appends($cause);
        return $data;
    }
    /*规格查询*/
    public function pay($where=null,$state=null)
    {
        $session=$this->session();
        $where['shop_id']=$session['id'];
        $where['status']=1;
        $category=DB::name('pay')->where($where)->select()->toArray();
        $data['id']=0;
        $data['name']='全部';
        if($state == 1)
        {
            if(!isset($where['id']))
            {
                foreach ($category as $k=>$v)
                {
                    $category[0]=$data;
                    $category[$k+1]=$v;
                }
            }
        }
        return $category;
    }
    /*查询总金额*/
    public function total($cause)
    {
        $session=$this->session();
        $where['shop_id']=$session['id'];
        if(isset($cause['pay_type']))
        {
            if($cause['pay_type']>0)
            {
                $where['pay_type']=$cause['pay_type'];
            }
        }
        if(isset($cause['order_no']))
        {
           if($cause['order_no'])
           {
               $where['order_no']=$cause['order_no'];
           }
        }
        if(isset($cause['pay_status']))
        {
            if($cause['pay_status']!=0)
            {
                $where['pay_status']=$cause['pay_status'];
            }
        }
        if(isset($cause['refunt']))
        {
            if($cause['refunt']!=0)
            {
                $where['refunt']=$cause['refunt'];
            }
        }
        $total_money=DB::name('order')->where($where)->field('sum(total_money) as total_money,sum(actual_money) as actual_money')->select()->toArray();
        return $total_money;
    }
    /*查询商品详情*/
    public function details($array)
    {
       $session=$this->session();
       $where['od.shop_id']=$session['id'];
       $where['od.order_num']=$array['id'];
       $date=DB::name('order_detail')->alias('od')->join('format f','f.f_id = od.for_matid')->where($where)->field('od.*,f.f_name')->paginate(10);
       if($date->toArray())
       {
           $date->toArray();
           foreach($date as $k=>$v)
           {
               $data=array();
               $data=$v;
               if($data['nature']!=0)
               {
                   $nature=DB::name('goods_backup')->where('id',$data['nature'])->field('state_name')->find();
                   $data['nature_name']=$nature['state_name'];
               }else{
                   $data['nature_name']='无';
               }
               if($data['integral']==1)
               {
                   $data['integral_name']='积分';
               }else{
                   $data['integral_name']='正常';
               }
               if($data['stemeal']==1)
               {
                   $data['stemeal_name']='单品';
               }else{
                   $data['stemeal_name']='套餐';
               }
               $date->offsetSet($k,$data);
           }
       }
       return$date;
    }
}